<script setup>
import { ref } from "vue";
import { useAuthStore } from "@/stores";
import { useRouter } from "vue-router";
import { ElMessage } from "element-plus";

// 使用ref跟踪当前显示的表单
const activeForm = ref("login");

// 切换表单的方法
const toggleForm = (formName) => {
  activeForm.value = formName;
};

const authStore = useAuthStore();
const router = useRouter();
const email = ref("");
const password = ref("");

//  登录
const handleLogin = async (e) => {
  e.preventDefault();
  try {
    await authStore.login(email.value, password.value);
    ElMessage.success("登录成功");
    router.push("/");
  } catch (err) {
    ElMessage.error("登录失败");
  }
};

//   注册
const handleRegister = async (e) => {
  e.preventDefault();
  try {
    // 调用注册接口或 Pinia action
    // await authStore.register(email.value, password.value);
    ElMessage.success("注册成功");
    router.push("/"); // 注册后跳转
  } catch (err) {
    ElMessage.error("注册失败");
  }
};
</script>

<template>
  <!-- 返回按钮 -->
  <a href="/"><button class="btn-home">返回首页</button></a>

  <div class="center">
    <div class="main-wrapper" id="mainWrapper">
      <div class="left-logo">
        <img src="../../assets/img/main_logo.png" alt="Stardew Valley Logo" />
      </div>
      <div class="right-form">
        <!-- 登录表单 - 根据activeForm值显示/隐藏 -->
        <form class="form-box" id="loginBox" v-show="activeForm === 'login'">
          <h2>登录账号</h2>

          <div class="input-group">
            <label class="input-label" for="email">邮箱</label>
            <input
              class="input-field"
              type="text"
              id="email"
              name="email"
              placeholder="请输入邮箱"
              v-model="email"
            />
          </div>
          <div class="input-group">
            <label class="input-label" for="password">密码</label>
            <input
              class="input-field"
              type="password"
              id="password"
              name="password"
              placeholder="请输入密码"
              v-model="password"
            />
          </div>

          <button class="btn" type="button" @click="handleLogin">登录</button>
          <div class="form-footer">
            还没有账号？
            <a @click="toggleForm('register')">注册</a>
          </div>
        </form>

        <!-- 注册表单 - 根据activeForm值显示/隐藏 -->
        <form class="form-box" id="registerBox" v-show="activeForm === 'register'">
          <h2>注册账号</h2>
          <div class="input-group">
            <label class="input-label" for="reg-username">用户名</label>
            <input
              class="input-field"
              type="text"
              id="reg-username"
              name="reg-username"
              placeholder="请输入用户名"
            />
          </div>
          <div class="input-group">
            <label class="input-label" for="reg-password">密码</label>
            <input
              class="input-field"
              type="password"
              id="reg-password"
              name="reg-password"
              placeholder="请输入密码"
            />
          </div>
          <div class="input-group">
            <label class="input-label" for="reg-confirm">确认密码</label>
            <input
              class="input-field"
              type="password"
              id="reg-confirm"
              name="reg-confirm"
              placeholder="请再次输入密码"
            />
          </div>
          <button class="btn" type="button" @click="handleRegister">注册</button>
          <div class="form-footer">
            已有账号？
            <a @click="toggleForm('login')">登录</a>
          </div>
        </form>
      </div>
    </div>
  </div>
</template>

<style scoped>
.btn-home {
  /* width: 100%; */
  padding: 10px;
  background: #e6c97a;
  color: #4a3a0a;
  border: 2px solid #b8860b;
  border-radius: 6px;
  font-size: 14px;
  font-family: inherit;
  font-weight: bold;
  cursor: pointer;
  letter-spacing: 1px;
  position: fixed;
  left: 1.25rem;
  top: 1.25rem;
}

.center {
  margin: 0;
  min-height: 100vh;
  height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;

  /* 背景图分层设置 */
  background-image: url("../../assets/img/sdv_divider.png"),
    url("../../assets/img/stardewbackground.png");
  /* 设置两张背景图位置 */
  background-size: 100% auto, cover;
  background-repeat: no-repeat;
  background-position: bottom, /* 上层图下移20px */ center calc(50% - 200px);
  /* 底层图上移20px */

  font-family: "Segoe UI", "Microsoft YaHei", Arial, sans-serif;
}

.main-wrapper {
  width: 900px;
  max-width: 98vw;
  height: 600px;
  max-height: 98vh;
  display: flex;
  box-shadow: 0 4px 32px rgba(0, 0, 0, 0.18);
  border-radius: 18px;
  overflow: hidden;
  background: rgba(255, 255, 255, 0.12);
  border: 2px solid #e6c97a;
  transition: flex-direction 0.6s cubic-bezier(0.77, 0, 0.18, 1);
}

.main-wrapper.reverse {
  flex-direction: row-reverse;
}

.left-logo {
  flex: 1.2;
  background: transparent;
  display: flex;
  align-items: center;
  justify-content: center;
  border-right: 2px solid #e6c97a;
  transition: border-right 0.6s cubic-bezier(0.77, 0, 0.18, 1),
    border-left 0.6s cubic-bezier(0.77, 0, 0.18, 1);
}

.main-wrapper.reverse .left-logo {
  border-right: none;
  border-left: 2px solid #e6c97a;
}

.left-logo img {
  width: 320px;
  max-width: 90%;
  height: auto;
  image-rendering: pixelated;
}

.right-form {
  flex: 1.5;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  background: rgba(255, 255, 255, 0.85);
  position: relative;
  transition: background 0.6s cubic-bezier(0.77, 0, 0.18, 1);
}

.form-box {
  width: 340px;
  height: 430px;
  background: #fffbe6;
  border-radius: 12px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.08);
  padding: 36px 32px;
  display: flex;
  flex-direction: column;
  align-items: stretch;
  border: 2.5px solid #e6c97a;
  transition: box-shadow 0.3s;
}

.form-box h2 {
  font-family: "Segoe UI", "Microsoft YaHei", Arial, sans-serif;
  font-size: 26px;
  color: #b8860b;
  margin-bottom: 18px;
  letter-spacing: 2px;
  width: 100%;
  text-align: center;
}

.divider {
  width: 180px;
  margin: 0 auto 18px auto;
  display: block;
}

.input-group {
  width: 100%;
  margin-bottom: 18px;
}

.input-label {
  font-size: 15px;
  color: #7a5c1b;
  margin-bottom: 6px;
  display: block;
}

.input-field {
  width: 100%;
  padding: 10px 12px;
  box-sizing: border-box;
  border: 2px solid #e6c97a;
  border-radius: 6px;
  font-size: 15px;
  background: #fffde4;
  outline: none;
  transition: border-color 0.2s;
  font-family: inherit;
  box-shadow: 0 1px 0 #e6c97a;
}

.input-field:focus {
  border-color: #b8860b;
}

.btn {
  width: 100%;
  padding: 10px 0;
  background: #e6c97a;
  color: #4a3a0a;
  border: 2px solid #b8860b;
  border-radius: 6px;
  font-size: 17px;
  font-family: inherit;
  font-weight: bold;
  cursor: pointer;
  margin-top: 8px;
  box-shadow: 0 2px 0 #e6c97a;
  letter-spacing: 1px;
  transition: background 0.2s, color 0.2s;
}

.btn:hover {
  background: #ffe28a;
  color: #b8860b;
}

.form-footer {
  margin-top: 18px;
  font-size: 14px;
  color: #7a5c1b;
  text-align: right;
  width: 100%;
}

.form-footer a {
  color: #b8860b;
  text-decoration: underline;
  margin-left: 4px;
  cursor: pointer;
}

@media (max-width: 700px) {
  .main-wrapper {
    flex-direction: column;
    width: 98vw;
    height: auto;
    min-height: 100vh;
  }

  .left-logo,
  .right-form {
    border: none;
    min-height: 180px;
  }

  .left-logo {
    border-bottom: 2px solid #e6c97a;
    justify-content: flex-start;
    padding-top: 32px;
  }

  .right-form {
    padding-bottom: 32px;
  }
}
</style>
